package cn.com.xf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.com.xf.dao.SmsSendDAO;
import cn.com.xf.dao.TnsendgoodsaccountDAO;
import cn.com.xf.domain.SmsSend;
import cn.com.xf.domain.Tnsendgoodsaccount;
import cn.com.xf.service.GoodsAccountSmsService;

@Service
public class GoodsAccountSmsServiceImpl implements GoodsAccountSmsService {
	@Autowired
	private TnsendgoodsaccountDAO tnsendgoodsaccountDAO;
	@Autowired
	private SmsSendDAO smsSendDAO;

	Log log = LogFactory.getLog(GoodsAccountSmsServiceImpl.class);

	@Override
	public void insertSendGoodsAccountSms() {
		List<Tnsendgoodsaccount> list = tnsendgoodsaccountDAO.selectUnSend();
		System.out.println(list.size() + "-------------");

		// 应该根据手机进行分类

		// 1.收集所有可能的手机
		List<String> phoneList = new ArrayList<String>();
		for (Tnsendgoodsaccount tn : list) {
			if (StringUtils.isNotBlank(tn.getSendphone())) {
				if (!phoneList.contains(tn.getSendphone())) {
					phoneList.add(tn.getSendphone());
				}
			}
		}

		// 2.按照手机排序
		ArrayList<SmsSend> smslist = new ArrayList<SmsSend>();
		List<Tnsendgoodsaccount> updateList = new ArrayList<Tnsendgoodsaccount>();
		for (String phone : phoneList) {
			List<Tnsendgoodsaccount> oneSiteList = new ArrayList<Tnsendgoodsaccount>();
			// 1.汇总形成单个站点的数据
			String content = "";
			for (Tnsendgoodsaccount tn : list) {
				if (StringUtils.isNotBlank(tn.getSendphone())) {
					if (phone.equalsIgnoreCase(tn.getSendphone())) {
						oneSiteList.add(tn);
					}
				}
			}
			if (CollectionUtils.isEmpty(oneSiteList))
				continue;
			String msgid = UUID.randomUUID().toString().replaceAll("-", "");
			if (oneSiteList.size() == 1) {
				Tnsendgoodsaccount t = oneSiteList.get(0);
				String orderId = t.getOrderid();
				String amount = t.getGoodsmoney() + "";
				String date = "今天";
				if (null != oneSiteList.get(0).getRettime()) {
					date = DateFormatUtils.format(oneSiteList.get(0).getRettime(), "yyyy年MM月dd日");
				}
				content = "尊敬的客户:您好，您的寄件单号" + orderId + "(" + amount + "元)的代收货款已于" + date
						+ "返款给您;如有疑问请致电:0769-82880914;谢谢您使用信丰物流。回复TD退订";
				// 回写
				t.setMsgid(msgid);
				updateList.add(t);
			} else if (oneSiteList.size() > 1) {
				String sum = oneSiteList.size() + "";
				double account = 0;
				Date d = null;
				for (Tnsendgoodsaccount tac : oneSiteList) {
					account = account + Double.valueOf(tac.getGoodsmoney() + "");
					if (null != tac.getRettime()) {
						d = tac.getRettime();
					}
					// 回写
					tac.setMsgid(msgid);
					updateList.add(tac);
				}
				String date = "今天";
				if (null != d) {
					date = DateFormatUtils.format(d, "yyyy年MM月dd日");
				}
				content = "尊敬的客户:您好，您的寄件共" + sum + "票,共计:" + account + "元的代收货款已于" + date
						+ "返款给您;如有疑问请致电:0769-82880914;谢谢您使用信丰物流。回复TD退订";
			}
			SmsSend smsSend = new SmsSend();
			smsSend.setMsgRefid(msgid);
			smsSend.setMsgContent(content);
			smsSend.setMsgMobile(phone);
			smsSend.setMsgStatus(Short.valueOf("0"));
			smsSend.setMsgType("9");
			smsSend.setMsgEntertimeStr(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			smslist.add(smsSend);
		}
		smsSendDAO.insertBatch(smslist);
		// 回写tnsendgoodsaccount
		tnsendgoodsaccountDAO.updateBatch(updateList);
		log.warn("成功提交" + list.size() + "条短信记录");
	}
}
